<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()">
          <i class="el-icon-back" />
          返回
        </span>
        <span>|</span>
        <span>编辑企业</span>
      </div>
      <div class="right">黑马程序员</div>
    </header>
    <main class="add-main">
      <div class="add-enterprise-container">
        <div>企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :model="addForm" :rules="addRules">
            <el-row>
              <el-col :span="12">
                <el-form-item label="企业名称" prop="name">
                  <el-input v-model="addForm.name" placeholder="请输入企业名称" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="法人" prop="legalPerson">
                  <el-input v-model="addForm.legalPerson" placeholder="请输入法人" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="注册地址" prop="registeredAddress">
                  <el-input v-model="addForm.registeredAddress" placeholder="请输入注册地址" />
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="所在行业" prop="industryCode">
                  <el-select v-model="addForm.industryCode" placeholder="请选择">
                    <el-option
                      v-for="item in industryList"
                      :key="item.industryCode"
                      :label="item.industryName"
                      :value="item.industryCode"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item v-model="addForm.contact" label="企业联系人" prop="name">
                  <el-input v-model="addForm.contact" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系电话" prop="contactNumber">
                  <el-input v-model="addForm.contactNumber" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row class="upload-container">

              <el-form-item label="营业执照" prop="businessLicenseId" :span="3">
                <el-upload
                  class="upload-demo"
                  action="#"
                  :http-request="uploadRequest"
                  :before-upload="beforeUpload"
                >
                  <el-button size="small" type="primary" plain class="upload-btn">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">支持扩展名：.png .jpg .jpeg，文件大小不得超过5M</div>
                </el-upload>

              </el-form-item>

            </el-row>

          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary" @click="confirmSubmit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getIndustryList, uploadAPI, updateEnterprise, getEnterpriseInfo } from '@/api/enperprise'
const validatePhone = (rule, value, callback) => {
  if (/^1[3-9]\d{9}$/.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的手机号'))
  }
}
export default {
  data() {
    // 自定义校验

    return {

      enterpriseId: '',
      industryList: [],
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },
      addRules: {
        name: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }

        ],
        legalPerson: [
          { required: true, message: '请输入法人', trigger: 'blur' }
        ],
        registeredAddress: [
          { required: true, message: '请输入注册地址', trigger: 'blur' }
        ],
        industryCode: [
          { required: true, message: '请选择行业', trigger: 'change' }
        ],
        contact: [
          { required: true, message: '请输入企业联系人', trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { validator: validatePhone, trigger: 'blur' }
        ],

        businessLicenseId: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ]

      }

    }
  },
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  created() {
    this.getIndustryList()
    this.getEnterpriseDetail()
  },
  methods: {
    async getIndustryList() {
      const res = await getIndustryList()
      this.industryList = res.data
    },
    async uploadRequest(data) {
      // console.log(data)
      const file = data.file
      const formData = new FormData()
      formData.append('file', file)
      formData.append('type', 'businessLicense')
      const res = await uploadAPI(formData)
      //   console.log(res)

      this.addForm.businessLicenseUrl = res.data.url
      this.addForm.businessLicenseId = res.data.id

      // 手动触发上传文件的表单校验
      this.$refs.ruleForm.validateField('businessLicenseId')

      // const res = await uploadAPI()
      // console.log(res)
    },

    // 上传文件之前的钩子
    beforeUpload(file) {
      const allowImgType = ['image/jpeg', 'image/png', 'image/jpg'].includes(file.type)
      const isLt5M = file.size / 1024 / 1024 < 5

      if (!allowImgType) {
        this.$message.error('上传合同图片只能是 PNG/JPG/JPEG 格式!')
      }
      if (!isLt5M) {
        this.$message.error('上传合同图片大小不能超过 5MB!')
      }
      return allowImgType && isLt5M
    },
    confirmSubmit() {
      this.$refs.ruleForm.validate(async(result) => {
        // console.log(result)
        if (!result) return
        await updateEnterprise(this.addForm)
        //  返回列表页
        this.$router.back()
      })
    },
    async getEnterpriseDetail() {
      const res = await getEnterpriseInfo(this.id)
      console.log(res)
      const { name, legalPerson, registeredAddress, industryCode, contact, contactNumber, businessLicenseUrl, businessLicenseId, id } = res.data
      this.addForm = {
        id,
        name,
        legalPerson,
        registeredAddress,
        industryCode,
        contact,
        contactNumber,
        businessLicenseUrl,
        businessLicenseId
      }
    }

  }
}
</script>

  <style scoped lang="scss">
  .add-enterprise {
    background-color: #f4f6f8;
    height: 100vh;
  }
  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    justify-content: space-between;
    background-color: #fff;
  }
  .left {
    span {
      margin-right: 5px;
    }
  }
  .add-main {
    background-color: #f4f6f8;
    padding: 20px 130px;
  }
  .add-enterprise-container {
    background-color: #fff;
    padding: 20px 20px;
    margin-bottom: 20px;
  }
  .form {
    text-align: center;
    padding: 30px;
  }
  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 25px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  ::v-deep .el-select {
    width: 421.67px;
  }
  .upload-container {
    display: flex;
    justify-content: flex-start;
  }
  ::v-deep .upload-demo {
    margin-left: 0px;
    text-align: left;
  }
  ::v-deep .el-upload{
    text-align: left;

  }
  </style>
